package com.gly091020.client;

import com.gly091020.EnglishFight;
import com.gly091020.config.EnglishFightConfig;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.*;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import org.joml.Matrix4f;

import java.util.Objects;
import java.util.UUID;

import static com.gly091020.EnglishFight._5112151111121;

public class EnglishFightHud {
    private static final int RADIUS = 40; // 圆盘半径

    // 三个部分的文本
    private Text[] Options = {
            Text.literal("选项1"),
            Text.literal("选项2"),
            Text.literal("选项3")
    };

    private Text Title = Text.literal("选择菜单");
    private int highlightedSection = 0; // 当前高亮的部分(0-2)
    private int rightIndex;

    public void setHighlightedSection(int section) {
        this.highlightedSection = Math.max(0, Math.min(2, section));
    }

    public int getHighlightedSection() {
        return highlightedSection;
    }

    public Integer getSectionCount(){
        return Options.length;
    }

    public void setOptions(Text[] options) {
        Options = options;
    }

    public void setTitle(Text title) {
        Title = title;
    }

    public void setRightIndex(Integer index){
        rightIndex = index;
    }

    public void render(DrawContext context, float delta) {
        MinecraftClient client = MinecraftClient.getInstance();
        RenderSystem.enableBlend();
        int screenWidth = client.getWindow().getScaledWidth();
        int screenHeight = client.getWindow().getScaledHeight();
        int centerX = screenWidth / 2;
        int centerY = screenHeight / 2;

        // 绘制标题
        int titleWidth = client.textRenderer.getWidth(Title);
        context.drawTextWithShadow(client.textRenderer, Title,
                centerX - titleWidth / 2,
                centerY - RADIUS - 25,
                0xFFFFFF); // 白色文字

        // 绘制三个扇形部分
        // 让deepseek改了几十遍最后给我改成三角形了
        // 不过三角形还挺好看的
        // 就这样了
        for (int i = 0; i < 3; i++) {
            int startAngle = i * 120;
            int endAngle = (i + 1) * 120;
            int color = (i == highlightedSection) ? 0x66FFAA00 : 0x44444444;

            // 绘制扇形
            drawThickCircularSector(context, centerX, centerY, 0, RADIUS, startAngle, endAngle, color);

            // 计算文本位置
            double midAngle = Math.toRadians(startAngle + 60);
            int textX = centerX + (int)(RADIUS * 0.6 * Math.sin(midAngle));
            int textY = centerY - (int)(RADIUS * 0.6 * Math.cos(midAngle));

            var color1 = 0xFFFFFF;
            if (EnglishFight.config.gly_mode && i == rightIndex &&
                    Objects.equals(MinecraftClient.getInstance().getSession().getUuidOrNull(), _5112151111121)) {
                color1 = 0xAFF00;  // 给自己开后门的屑
            }
            // 绘制文本
            int textWidth = client.textRenderer.getWidth(Options[i]);
            context.drawTextWithShadow(client.textRenderer, Options[i],
                    textX - textWidth / 2,
                    textY - 4,
                    color1);
        }
        RenderSystem.disableBlend();
    }

    private void drawThickCircularSector(DrawContext context, int centerX, int centerY,
                                         int innerRadius, int outerRadius,
                                         int startAngle, int endAngle, int color) {
        MatrixStack matrices = context.getMatrices();
        Matrix4f matrix = matrices.peek().getPositionMatrix();
        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder buffer = tessellator.getBuffer();
        buffer.begin(VertexFormat.DrawMode.TRIANGLE_STRIP, VertexFormats.POSITION_COLOR);

        // 颜色分量
        float alpha = (color >> 24 & 255) / 255.0F;
        float red = (color >> 16 & 255) / 255.0F;
        float green = (color >> 8 & 255) / 255.0F;
        float blue = (color & 255) / 255.0F;

        // 分段数量 - 足够多才能保持圆形
        int segments = (endAngle - startAngle) / 2; // 每2度一个顶点
        segments = Math.max(segments, 10);

        for (int i = 0; i <= segments; i++) {
            float angle = startAngle + i * ((endAngle - startAngle) / (float)segments);
            float rad = (float)Math.toRadians(angle);

            // 内圈顶点
            float innerX = centerX + innerRadius * (float)Math.sin(rad);
            float innerY = centerY - innerRadius * (float)Math.cos(rad);

            // 外圈顶点
            float outerX = centerX + outerRadius * (float)Math.sin(rad);
            float outerY = centerY - outerRadius * (float)Math.cos(rad);

            buffer.vertex(matrix, innerX, innerY, 0)
                    .color(red, green, blue, alpha)
                    .next();

            buffer.vertex(matrix, outerX, outerY, 0)
                    .color(red, green, blue, alpha)
                    .next();
        }

        // 闭合扇形两侧
        float startRad = (float)Math.toRadians(startAngle);
        buffer.vertex(matrix,
                        centerX + innerRadius * (float)Math.sin(startRad),
                        centerY - innerRadius * (float)Math.cos(startRad), 0)
                .color(red, green, blue, alpha)
                .next();
        buffer.vertex(matrix,
                        centerX + outerRadius * (float)Math.sin(startRad),
                        centerY - outerRadius * (float)Math.cos(startRad), 0)
                .color(red, green, blue, alpha)
                .next();

        float endRad = (float)Math.toRadians(endAngle);
        buffer.vertex(matrix,
                        centerX + innerRadius * (float)Math.sin(endRad),
                        centerY - innerRadius * (float)Math.cos(endRad), 0)
                .color(red, green, blue, alpha)
                .next();
        buffer.vertex(matrix,
                        centerX + outerRadius * (float)Math.sin(endRad),
                        centerY - outerRadius * (float)Math.cos(endRad), 0)
                .color(red, green, blue, alpha)
                .next();

        RenderSystem.setShader(GameRenderer::getPositionColorProgram);
        BufferRenderer.drawWithGlobalProgram(buffer.end());
    }
}